//
//  BarView.m
//  DebugDemo
//
//  Created by XFB on 2019/5/16.
//  Copyright © 2019 ZhouCheng. All rights reserved.
//

#import "BarView.h"
#import "NSString+Size.h"
#import "SingleBarView.h"

@interface BarView ()
{
    CGRect bezierFrame;
}

@property (nonatomic, weak) CALayer *contentLayer;

@end

@implementation BarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        bezierFrame = frame;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.layer addSublayer:self.contentLayer];
    /// 内容布局
    [self layoutContent];
}

- (void)layoutContent
{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGRectGetWidth(bezierFrame), CGRectGetHeight(bezierFrame)), NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1);
    [[UIColor blackColor] setStroke];

    /// 画Y轴
    CGContextSetLineWidth(context, 1);
    CGContextMoveToPoint(context, 30, 0);
    CGContextAddLineToPoint(context, 30, CGRectGetHeight(bezierFrame)-30);
    CGContextStrokePath(context);

    /// 画X轴 细节
    CGSize lastStringSize = [self.x_names.lastObject sizeWithContentFont:[UIFont systemFontOfSize:10] limitWidth:MAXFLOAT];
    CGFloat x_totalWidth = CGRectGetWidth(bezierFrame)-30-lastStringSize.width/2 ;
    CGContextSetLineWidth(context, 1);
    CGContextMoveToPoint(context, 30, CGRectGetHeight(bezierFrame)-30);
    CGContextAddLineToPoint(context, x_totalWidth+30, CGRectGetHeight(bezierFrame)-30);
    CGContextStrokePath(context);

    /// X轴箭头
    CGContextSetLineWidth(context, 1);
    CGContextMoveToPoint(context, x_totalWidth+30-10, CGRectGetHeight(bezierFrame)-30-5);
    CGContextAddLineToPoint(context, x_totalWidth+30, CGRectGetHeight(bezierFrame)-30);
    CGContextMoveToPoint(context, x_totalWidth+30-10, CGRectGetHeight(bezierFrame)-30+5);
    CGContextAddLineToPoint(context, x_totalWidth+30, CGRectGetHeight(bezierFrame)-30);
    
    /// Y轴箭头
    CGContextSetLineWidth(context, 1);
    CGContextMoveToPoint(context, 30, 0);
    CGContextAddLineToPoint(context, 30-5, 10);
    CGContextMoveToPoint(context, 30, 0);
    CGContextAddLineToPoint(context, 30+5, 10);
    
    /// Y轴额外的点
    CGFloat cellHeight = (CGRectGetHeight(bezierFrame)-60)/10;
    for (NSInteger i = 0; i < 11; i ++) {
        CGContextSetLineWidth(context, 1);
        CGContextMoveToPoint(context, 30+3, cellHeight*i+30);
        CGContextAddLineToPoint(context, 30, cellHeight*i+30);
        CGContextStrokePath(context);
    }
    
    /// X轴额外的点
    CGFloat cellWidth = x_totalWidth / (self.x_names.count+1);
    for (int i = 0; i < _x_names.count; i ++) {
        CGContextSetLineWidth(context, 1);
        CGContextMoveToPoint(context, 30 + cellWidth*(i+1), CGRectGetHeight(bezierFrame)-30);
        CGContextAddLineToPoint(context, 30 +cellWidth*(i+1), CGRectGetHeight(bezierFrame)-30-3);
        CGContextStrokePath(context);
    }
    
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;

    /// 画X轴坐标标题
    CGFloat allVerExtHeight = 0;
    for (int i = 0; i < self.x_names.count; i ++) {
        
        NSString *titleString = self.x_names[i];
        NSDictionary *attributes = @{
                                     NSFontAttributeName:[UIFont systemFontOfSize:10],
                                     NSForegroundColorAttributeName:[UIColor blackColor],
                                     NSParagraphStyleAttributeName:paragraphStyle
                                     };
        CGSize titleSize = [titleString sizeWithContentFont:[UIFont systemFontOfSize:10] limitWidth:MAXFLOAT];
        [titleString drawInRect:CGRectMake(30 + allVerExtHeight - titleSize.width/2 + cellWidth,
                                           CGRectGetHeight(bezierFrame)-30 + (30-titleSize.height)/2,
                                           titleSize.width,
                                           titleSize.height) withAttributes:attributes];
        allVerExtHeight = allVerExtHeight + cellWidth;
    }

    // Y轴坐标标题
    CGFloat allHorExtHeight = 0;
    for (NSInteger i = 0; i < 11; i ++) {
        CGFloat y = CGRectGetHeight(bezierFrame)-cellHeight*i-30;

        NSString *titleString = [NSString stringWithFormat:@"%ld",10*i];
        NSDictionary *attributes = @{
                                     NSFontAttributeName:[UIFont systemFontOfSize:10],
                                     NSForegroundColorAttributeName:[UIColor blackColor],
                                     NSParagraphStyleAttributeName:paragraphStyle
                                     };
        [titleString drawInRect:CGRectMake(0, y-6, 30, 10) withAttributes:attributes];
        allHorExtHeight = allHorExtHeight + cellHeight;
    }

    UIImage * currentImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.contentLayer.contents = (__bridge id _Nullable)(currentImage.CGImage);
}

- (void)show
{
    CGFloat count = _x_names.count;
    CGSize lastStringSize = [self.x_names.lastObject sizeWithContentFont:[UIFont systemFontOfSize:10] limitWidth:MAXFLOAT];
    CGFloat x_totalWidth = CGRectGetWidth(bezierFrame)-30-lastStringSize.width/2 ;
    
    CGFloat barW = 20;
    CGFloat barY = 30;
    CGFloat barH = CGRectGetHeight(bezierFrame)-30-30;
    /// 两个Bar的间距
    CGFloat barMargin = x_totalWidth / (self.x_names.count+1);
    
    for (NSInteger i = 0; i < count; i ++) {
        CGFloat barX = 20 + barMargin * (i + 1);
        SingleBarView *singleBar = [[SingleBarView alloc] initWithFrame:CGRectMake(barX, barY, barW, barH)];
        singleBar.barWidth = barW;
        singleBar.barColor = RandomColor;
        CGFloat yValue = [_y_values[i] floatValue];
        singleBar.barProgress = isnan(yValue/100)? 0:(yValue/100);
        [self addSubview:singleBar];
    }
}


#pragma mark --- <Getter And Setter>

- (CALayer *)contentLayer
{
    if (!_contentLayer) {
        CALayer *contentLayer = [CALayer layer];
        contentLayer.backgroundColor = [UIColor clearColor].CGColor;
        /// 不是frame 是bounds 有锚点影响
        contentLayer.frame = self.bounds;
        _contentLayer = contentLayer;
    }
    return _contentLayer;
}

@end
